package com.ambercrm.dialog_utils;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ambercrm.R;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 底部弹出菜单Utils
 * Created by hui.feng on 2018/1/19.
 */
public class BottomDialogTools {

    private static Dialog mCameraDialog;

    public static Dialog showBottomDialogText(Context context, boolean isTouchHide, final ArrayList<HashMap<String, String>> list, String titleText, final BottomDialogAdapter.OnPopupClickListener listener) {
        mCameraDialog = new Dialog(context, R.style.BottomDialog);
        View root = LayoutInflater.from(context).inflate(
                R.layout.bottom_dialog_layout, null);
        mCameraDialog.setContentView(root);
        RecyclerView mLvContent = root.findViewById(R.id.lv_contentView);
        TextView mTitleText = root.findViewById(R.id.title_text);
        mTitleText.setText(titleText);
        BottomDialogAdapter adapter = new BottomDialogAdapter(R.layout.adapter_bottom_dialog, list);
        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        mLvContent.setLayoutManager(layoutManager);
        mLvContent.setHasFixedSize(true);
        mLvContent.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (mCameraDialog != null) {
                    mCameraDialog.dismiss();
                }
            }
        });

        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.rl_des) {
                    if (listener != null) {
                        listener.onClick(position, mCameraDialog);
                    }
                }
            }
        });
        mCameraDialog.setCanceledOnTouchOutside(isTouchHide);
        Window dialogWindow = mCameraDialog.getWindow();
        if (null == dialogWindow) {
            return mCameraDialog;
        }
        dialogWindow.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        lp.x = 0; // 新位置X坐标
        lp.y = 0; // 新位置Y坐标
        lp.width = context.getResources().getDisplayMetrics().widthPixels; // 宽度
        root.measure(0, 0);
        lp.height = root.getMeasuredHeight();
        lp.alpha = 9f; // 透明度
        dialogWindow.setAttributes(lp);
        mCameraDialog.show();
        return mCameraDialog;
    }


    public static class BottomDialogAdapter extends BaseQuickAdapter<HashMap<String, String>, BaseViewHolder> {

        public BottomDialogAdapter(int layoutResId, @Nullable ArrayList<HashMap<String, String>> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, HashMap<String, String> item) {
            helper.setText(R.id.tv_dialog_text, item.get("title"));
            helper.addOnClickListener(R.id.rl_des);
        }

        public interface OnPopupClickListener {
            void onClick(int position, Dialog dialog);
        }
    }
}
